#!/bin/bash

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

yum install -y gcc gcc-c++ perl

#curl -SLO ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz --progress
#curl -SLO http://zlib.net/zlib-1.2.11.tar.gz --progress
#curl -SL -o openssl.tar.gz  https://github.com/openssl/openssl/archive/OpenSSL_1_0_2l.tar.gz --progress
#curl -SLO https://nginx.org/download/nginx-1.14.0.tar.gz --progress

curl -SLO http://192.168.2.60:9999/pcre-8.42.tar.gz --progress
curl -SLO http://192.168.2.60:9999/zlib-1.2.11.tar.gz --progress
curl -SL -o openssl.tar.gz http://192.168.2.60:9999/OpenSSL_1_0_2l.tar.gz --progress
curl -SLO http://192.168.2.60:9999/nginx-1.14.0.tar.gz --progress


tar xvf nginx-1.14.0.tar.gz
tar xvf pcre-8.42.tar.gz -C nginx-1.14.0
tar xvf zlib-1.2.11.tar.gz -C nginx-1.14.0
tar xvf openssl.tar.gz -C nginx-1.14.0

cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --with-pcre=./pcre-8.42 --with-zlib=./zlib-1.2.11 --with-http_ssl_module --with-stream --with-stream_ssl_module --with-openssl=./openssl-OpenSSL_1_0_2l
make && make install

echo "$HOSTNAME    `hostname -I`" >/usr/local/nginx/html/index.html
echo "include       /usr/local/nginx/tcp.d/*.conf;" >>/usr/local/nginx/conf/nginx.conf
mkdir /usr/local/nginx/tcp.d

cat <<!   >/usr/local/nginx/tcp.d/emqx_proxy.conf
stream {
    upstream backend {
        # hash $remote_addr consistent;
        server 192.168.2.71:80  max_fails=3 fail_timeout=30s;
        server 192.168.2.72:80  max_fails=3 fail_timeout=30s;
        server 192.168.2.73:80  max_fails=3 fail_timeout=30s;
    }
    server {
        listen 8888;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}
EOF

/usr/local/nginx/sbin/nginx
